package com.example.contactfragment;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;

import com.boyuan.huijiaoyu.Huodong;
import com.boyuan.huijiaoyu.R;
import com.boyuan.huijiaoyu.ZhuceYonghu;
import com.example.login.LoginApp1;
import com.example.nearby.NearbyCenter;
import com.example.newfragment.DetailPerson;
import com.example.newfragment.RecentPerson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.magicsoft.app.entity.MapListResp;
import com.magicsoft.app.helper.TokenHelper;
import com.magicsoft.app.wcf.AsyncHttpServiceHelper;
import com.magicsoft.app.wcf.BaseService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;

public class ContactService extends BaseService {

	public ContactService(Context context) {
		super(context);
	}
	
	
	/**
	 * 获取联系人详情
	 * 
	 * @param uid
	 *            当前用户唯一标示
	 * @param listener
	 */
	public void getContactdetail(String uid, String contactsID,
			final GetOneRecordListener<DetailPerson> listener) {

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_DETAIL2 + getParamString + "&uid="
				+ uid + "&contactsID=" + contactsID;

		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONObject contentObject = response.getJSONObject("content");

						Type contentType = new TypeToken<DetailPerson>() {
						}.getType();
						DetailPerson datas = gson.fromJson(
								contentObject.toString(), contentType);

						if (listener != null) {
							listener.onFinish(datas);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {
					if (listener != null)
						listener.onFailed(context.getResources().getString(// JSON
								// 解析错误
								R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString, throwable);
				if (listener != null){
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				if (listener != null){
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
				}
			}
		});

	}
	
	
	/**
	 * 添加常用联系人
	 * 
	 * @param uid
	 *            当前用户的唯一标示
	 * @param contactsID
	 *            需要添加联系人的唯一标示
	 * @param listener
	 */
	public void addContactFavorite(String uid, String contactsID,
			final GetOneRecordListener<String> listener) {
		RequestParams params = new RequestParams();

		params.put("uid", uid);
		params.put("contactsID", contactsID);
		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_CHANGYONG + getParamString;

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {

						if (listener != null) {
							listener.onFinish(message);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}


	/**
	 * 添加星标联系人
	 * 
	 * @param uid
	 *            当前用户的唯一标示
	 * @param contactsID
	 *            被联系人的唯一标示
	 * @param listener
	 */
	public void putContactFavorite(String uid, String contactsID,
			final GetOneRecordListener<String> listener) {
		RequestParams params = new RequestParams();

		params.put("uid", uid);
		params.put("contactsID", contactsID);
		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_ADDXINGBIAO + getParamString;

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {

						if (listener != null) {
							listener.onFinish(message);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});
	}

	/**
	 * 取消星标联系人
	 * 
	 * @param uid
	 *            当前用户的唯一标示
	 * @param contactsID
	 *            被联系人的唯一标示
	 * @param listener
	 */
	public void deleteContactFavorite(String uid, String contactsID,
			final GetOneRecordListener<String> listener) {
		RequestParams params = new RequestParams();

		params.put("uid", uid);
		params.put("contactsID", contactsID);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_ADDXINGBIAO + getParamString
				+ "&uid=" + uid + "&contactsID=" + contactsID;

		AsyncHttpServiceHelper.delete(requestUrl,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {

						if (listener != null) {
							listener.onFinish(message);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});
	}

	/**
	 * 获取常用联系人
	 * 
	 * @param uid
	 *            当前用户唯一标示
	 * @param listener
	 */
	public void getFrequentContacts(String uid,
			final GetOneRecordListener<List<DetailPerson>> listener) {

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_FREQUENT2 + getParamString
				+ "&uid=" + uid;
		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONArray contentArray = response
								.getJSONArray("content");
						
						List<DetailPerson> result = new ArrayList<DetailPerson>();
						if (contentArray.toString().length() > 5) {

							Type contentType = new TypeToken<List<DetailPerson>>() {
							}.getType();
							List<DetailPerson> datas = gson
									.fromJson(contentArray.toString(),
											contentType);

							result.addAll(datas);
						}

						if (listener != null) {
							listener.onFinish(result);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources().getString(// JSON
								// 解析错误
								R.string.json_analysis_wrong)); 
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString, throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}
	
	/**
	 * 获取星标联系人
	 * 
	 * @param uid
	 *            当前用户唯一标示
	 * @param listener
	 */
	public void getXingbiaoContacts(String uid,
			final GetOneRecordListener<List<DetailPerson>> listener) {

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_FREQUENT3 + getParamString
				+ "&uid=" + uid;

		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {

					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONArray contentArray = response
								.getJSONArray("content");

						List<DetailPerson> result = new ArrayList<DetailPerson>();
						if (contentArray.toString().length() > 5) {

							Type contentType = new TypeToken<List<DetailPerson>>() {
							}.getType();
							List<DetailPerson> datas = gson
									.fromJson(contentArray.toString(),
											contentType);

							result.addAll(datas);
						}

						if (listener != null) {
							listener.onFinish(result);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources().getString(// JSON
								// 解析错误
								R.string.json_analysis_wrong)); 
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString, throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}
	
	//注册用户
	public void zhuce(String account, String password, String mobile, String mail, final GetMultilParamsListener listener) {
		
		String requestUrl = Constant.ZHUCE;
		RequestParams params = new RequestParams();
		
		try {//
			params = TokenHelper.FetchColourToken6(context, true, account, password, mobile, mail);
		} catch (Exception e) {
		}
		
		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONObject content = response.getJSONObject("content");
						Type contentType = new TypeToken<ZhuceYonghu>() {
						}.getType();
						ZhuceYonghu yonghu = gson.fromJson(content.toString(), contentType);
						if (listener != null) {
							listener.onFinish(yonghu);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});
	}
	
	
	//用户登录
	public void getdenglu1(String username, String password,final GetMultilParamsListener listener) {
		
		String requestUrl = Constant.DENGLU;
		RequestParams params = new RequestParams();
		
		try {//
			params = TokenHelper.FetchColourToken1(context, true,username,password);
		} catch (Exception e) {
		}
		
		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					
					String code = response.getString("code");
					Log.e("code", code);
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONObject content = response.getJSONObject("content");
						Type contentType = new TypeToken<LognUser>() {
						}.getType();
						LognUser lognUser = gson.fromJson(content.toString(), contentType);
						if (listener != null) {
							listener.onFinish(lognUser);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});
	}
	
	//修改密码
	public void changeMima(String username, String oldpassword, String newpassword, final PostRecordResponseListener listener){
		String url = Constant.XIUGAIMIMA;
		RequestParams params = new RequestParams();
		try {
			params = TokenHelper.FetchColourToken4(context, true, username, oldpassword, newpassword);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		AsyncHttpServiceHelper.put(url , params , new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						if (listener != null) {
							listener.onFinish(message);
						}
					} else {
						if (listener != null) {
							listener.onFailed(message);
						}
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
			}
		});
	}
	
	//修改员工信息
		public void changePerson(String id, String mobile, String mail, String realname, String sex, String fixed_line, String id_card_no, String address, String education,final PostRecordResponseListener listener){
			String requestUrl = Constant.XIUGAI;
			RequestParams params = new RequestParams();	
			try {
				params = TokenHelper.FetchColourToken3(context, true, id, mobile, mail, realname, sex, fixed_line, id_card_no, address, education);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			AsyncHttpServiceHelper.put(requestUrl, params, new JsonHttpResponseHandler(){
				@Override
				public void onSuccess(int statusCode, Header[] headers,
						JSONObject response) {
					super.onSuccess(statusCode, headers, response);
					
					try {
						String code = response.getString("code");
						String message = response.getString("message");
						if ("0".equals(code)) {
							if (listener != null) {
								listener.onFinish(message);
							}
						} else {
							if (listener != null) {
								listener.onFailed(message);
							}
						}

					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
				@Override
				public void onFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONObject errorResponse) {
					super.onFailure(statusCode, headers, throwable, errorResponse);
				}
			});
		}
	
	//获取员工信息
	public void getEmployee(String accountid,final GetMultilParamsListener listener) {
		
		String params = "";
		try {
			params = TokenHelper.FetchColourToken2(context, accountid, true);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String requestUrl = Constant.EMPLOYEE + params;
		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONObject content = response.getJSONObject("content");
						Type contentType = new TypeToken<LoginApp1>() {
						}.getType();
						LoginApp1 employee = gson.fromJson(content.toString(), contentType);
						if (listener != null) {
							listener.onFinish(employee);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});
	}
	
	//获取用户信息
		public void getAccount(String account, final GetMultilParamsListener listener){
			String params = "";
			try {
				params = TokenHelper.FetchColourToken2(context, account, true);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String url = Constant.GETACCOUNT + params;
			
			AsyncHttpServiceHelper.get(url, new JsonHttpResponseHandler(){
				@Override
				public void onSuccess(int statusCode, Header[] headers,
						JSONObject response) {
					super.onSuccess(statusCode, headers, response);
					try {
						String code = response.getString("code");
						String message = response.getString("message");
						if("0".equals(code)){
							JSONObject content = response.getJSONObject("content");
							Type contentType = new TypeToken<LognUser>() {
							}.getType();
							LognUser account = gson.fromJson(content.toString(), contentType);
							if(listener != null){
								listener.onFinish(account);
							}
						}else{
							if(listener != null){
								listener.onFailed(message);
							}
						}
					} catch (JSONException e) {
						if (listener != null)
							listener.onFailed(context.getResources()
									.getString(// JSON
											// 解析错误
											R.string.json_analysis_wrong));
					}
				}
				
				@Override
				public void onFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONObject errorResponse) {
					super.onFailure(statusCode, headers, throwable, errorResponse);
					if (listener != null)
						listener.onFailed(context.getResources().getString(
								R.string.network_connections_wrong));
				}
			});
		}
	
	
	//获取附近中心
	public void getNearByCenterService(String uid,double lat,double lng,
			final GetOneRecordListener<List<NearbyCenter>> listener) {

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.NEARBYCENTER + getParamString
				+ "&uid=" + uid+"&location="+lng+"%2C"+lat+"&radius=10000";

		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {

					String code = response.getString("code");
					String message = response.getString("message");
					String contentEncrypt = response.getString("contentEncrypt");
					if ("0".equals(code)) {
						JSONArray contentArray = response
								.getJSONArray("content");

						List<NearbyCenter> result = new ArrayList<NearbyCenter>();
						if (contentArray.toString().length() > 5) {

							Type contentType = new TypeToken<List<NearbyCenter>>() {
							}.getType();
							List<NearbyCenter> datas = gson
									.fromJson(contentArray.toString(),
											contentType);

							result.addAll(datas);
						}

						if (listener != null) {
							listener.onFinish(result);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources().getString(// JSON
								// 解析错误
								R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString, throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}

	public void getSearchPeople(String keyword,
			final GetOneRecordListener<List<SearchPeople>> listener) {

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.SEARCH + getParamString
				+ "&keyword=" + keyword;

		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {

					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {
						JSONArray contentArray = response
								.getJSONArray("content");

						List<SearchPeople> result = new ArrayList<SearchPeople>();
						if (contentArray.toString().length() > 5) {
							Type contentType = new TypeToken<List<SearchPeople>>() {
							}.getType();
							List<SearchPeople> datas = gson
									.fromJson(contentArray.toString(),
											contentType);

							result.addAll(datas);
						}

						if (listener != null) {
							listener.onFinish(result);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources().getString(// JSON
								// 解析错误
								R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString, throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}
	/**
	 * 添加联系人记录
	 * 
	 * @param uid
	 *            当前用户的唯一标示
	 * @param contactsID
	 *            被联系人的唯一标示
	 * @param listener
	 */
	public void putFrequentContact(String uid, String contactsID,
			final GetOneRecordListener<String> listener) {
		RequestParams params = new RequestParams();

		params.put("uid", uid);
		params.put("contactsID", contactsID);
		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_FREQUENT + getParamString;

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {

					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {

						if (listener != null) {
							listener.onFinish(message);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}

	/**
	 * 删除常用联系人记录
	 * 
	 * @param uid
	 *            当前用户的唯一标示
	 * @param contactsID
	 *            被联系人的唯一标示
	 * @param listener
	 */
	public void deleteFrequentContact(String uid, String contactsID,
			final GetOneRecordListener<String> listener) {
//		RequestParams params = new RequestParams();
//
//		params.put("uid", uid);
//		params.put("contactsID", contactsID);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_FREQUENT + getParamString
				+ "&uid=" + uid + "&contactsID=" + contactsID;

		AsyncHttpServiceHelper.delete(requestUrl,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {

					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {

						if (listener != null) {
							listener.onFinish(message);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString,
						throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable,
						errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}

	/**
	 * 获取组织架构通讯录
	 * 
	 * @param orgID
	 *            组织id
	 * @param listener
	 */
	public void getOrgChildDatas(String orgID,
			final GetOneRecordListener<List<RecentPerson>> listener) {

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.CONTACT_CHILD_DATAS + getParamString
				+ "&orgID=" + orgID;

		AsyncHttpServiceHelper.get(requestUrl, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {

				super.onSuccess(statusCode, headers, response);
				try {

					String code = response.getString("code");
					String message = response.getString("message");
					if ("0".equals(code)) {

						JSONArray contentArray = response
								.getJSONArray("content");

						List<RecentPerson> result = new ArrayList<RecentPerson>();
						if (contentArray.toString().length() > 5) {

							Type contentType = new TypeToken<List<RecentPerson>>() {
							}.getType();
							List<RecentPerson> datas = gson.fromJson(
									contentArray.toString(), contentType);

							result.addAll(datas);
						}

						if (listener != null) {
							listener.onFinish(result);
						}
					} else {
						if (listener != null)
							listener.onFailed(message);
					}

				} catch (Exception e) {

					if (listener != null)
						listener.onFailed(context.getResources().getString(// JSON
								// 解析错误
								R.string.json_analysis_wrong));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {

				super.onFailure(statusCode, headers, responseString, throwable);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				if (listener != null)
					listener.onFailed(context.getResources().getString(
							R.string.network_connections_wrong));
			}
		});

	}
	
	
	//获取摇一摇信息
			public void getYaoyiyao(String url, String id, final GetMultilParamsListener listener){
				String params = "";
				try {
					params = TokenHelper.FetchColourToken_yaoyiyao(context, true);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String url1 = url +id+ params;
				
				AsyncHttpServiceHelper.get(url1, new JsonHttpResponseHandler(){
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							String code = response.getString("code");
							String message = response.getString("message");
							if("0".equals(code)){
								JSONObject content = response.getJSONObject("content");
								Type contentType = new TypeToken<Huodong>() {
								}.getType();
								Huodong huodong = gson.fromJson(content.toString(), contentType);
								if(listener != null){
									listener.onFinish(huodong);
								}
							}else{
								if(listener != null){
									listener.onFailed(message);
								}
							}
						} catch (JSONException e) {
							if (listener != null)
								listener.onFailed(context.getResources()
										.getString(// JSON
												// 解析错误
												R.string.json_analysis_wrong));
						}
					}
					
					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						super.onFailure(statusCode, headers, throwable, errorResponse);
						if (listener != null)
							listener.onFailed(context.getResources().getString(
									R.string.network_connections_wrong));
					}
				});
			}
		
	//获取搜一搜列表
	public void FindSearch(String url, int pageID, int pageSize, final GetOneRecordListener<List<Huodong>> listener ){
		String params = "";
			try {
				params = TokenHelper.FetchColourToken_souyisou(context, false, pageID, pageSize);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		String url1 = url + params;
		AsyncHttpServiceHelper.get(url1, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String code = response.getString("code");
					String message = response.getString("message");
					if("0".equals(code)){
						JSONObject jsonObject = response.getJSONObject("content");
						JSONObject list = jsonObject.getJSONObject("list");
						Iterator<String> keys = list.keys();
						List<Huodong> huodongList = new ArrayList<Huodong>();
						while(keys.hasNext()){
							JSONObject jsonObject2 = list.getJSONObject(keys.next());
							Type contentType = new TypeToken<Huodong>() {
							}.getType();
							Huodong huodong = gson.fromJson(jsonObject2.toString(), contentType);
							huodongList.add(huodong);
						}
						
						if(listener != null){
							listener.onFinish(huodongList);
						}
					}else{
						if(listener != null){
							listener.onFailed(message);
						}
					}
				} catch (JSONException e) {
					if (listener != null)
						listener.onFailed(context.getResources()
								.getString(// JSON
										// 解析错误
										R.string.json_analysis_wrong));
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(statusCode, headers, throwable, errorResponse);
			}
		});
	}

}
